export default class Sprite {
  constructor(option) {
    this.img = option.img;
    this.sx = option.sx;
    this.sy = option.sy;
    this.w = option.w;
    this.h = option.h;
    this.x = option.x;
    this.y = option.y;
    this.dx = option.dx;
    this.dy = option.dy;
    this.radius = option.radius;
    this.rotation = option.rotation;
    this.scale = option.scale;
  }

  // 碰撞检测
  collision(body) {
    let left, top, right, bottom;
    let Collided = {
      collided: false,
      position: ''
    }
    if (body.x + body.w >= this.x && body.x > this.x) {
      left = true;
    }
    if (body.y + body.h >= this.y && body.y > this.y) {
      top = true;
    }
    if (body.x <= this.x + this.w && body.x < this.x) {
      right = true;
    }
    if (body.y <= this.y + this.h && body.y < this.y) {
      bottom = true;
    }
    if (left || top || right || bottom) {
      if (left || right) {
        Collided.collided = true;
        Collided.position = 'x';
      } else if (top || bottom) {
        Collided.collided = true;
        Collided.position = 'y';
      }
    }
    return Collided;
  }
  // 碰撞检测
  collisionArc(body) {
    let left, top, right, bottom;
    let Collided = {
      collided: false,
      position: ''
    }
    if (
      body.x + body.radius >= this.x &&
      body.x + body.radius <= this.x &&
      body.y + body.radius >= this.y &&
      body.y - body.radius <= this.y
    ) {
      left = true;
    }
    if (
      body.y + body.radius >= this.y &&
      body.y + body.radius <= this.y &&
      body.x + body.radius >= this.x &&
      body.x - body.radius <= this.x
    ) {
      top = true;
    }
    if (
      body.x - body.radius <= this.x + this.w &&
      body.x - body.radius >= this.x &&
      body.y + body.radius >= this.y &&
      body.y - body.radius <= this.y
    ) {
      right = true;
    }
    if (
      body.y <= this.y + this.h &&
      body.y - body.radius >= this.y &&
      body.x + body.radius >= this.x &&
      body.x - body.radius <= this.x
    ) {
      bottom = true;
    }
    if (left || top || right || bottom) {
      if (left || right) {
        Collided.collided = true;
        Collided.position = 'y';
      } else if (top || bottom) {
        Collided.collided = true;
        Collided.position = 'x';
      }
    }
    return Collided;
  }
}